﻿using MsgExample;
using PENet;
using System;
using System.Threading.Tasks;

namespace ClientExample
{
    public class ClientStart
    {
        static KCPNet<ClientSession, NetMsg> client;
        static void Main(string[] args)
        {
            string ip = "192.168.93.2";
            client = new KCPNet<ClientSession, NetMsg>();
            client.StartAsClient(ip, 17666);

            Task.Run(async () =>
            {
                var res = await client.AsyncConnectServer();
                if (res)
                    _ = Task.Run(SendPingMsg);
            });

            while (true)
            {
                string ipt = Console.ReadLine();
                if (ipt == "quit")
                {
                    client.CloseClient();
                    break;
                }
                else
                {
                    client.clientSession.SendMsg(new NetMsg
                    {
                        info = ipt
                    });
                }
            }

            Console.ReadKey();
        }


        static async void SendPingMsg()
        {
            while (true)
            {
                await Task.Delay(5000);
                if (client != null && client.clientSession != null)
                {
                    client.ClientSendMsg(new NetMsg
                    {
                        cmd = CMD.NetPing,
                        netPing = new NetPing
                        {
                            isOver = false
                        }
                    });
                    KCPTool.Log("Send Ping...");
                }
                else
                {
                    KCPTool.Log("Ping Task Cancel");
                    break;
                }
            }
        }
    }
}
